define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'task/record_money/index',
                    add_url: 'task/record_money/add',
                    edit_url: 'task/record_money/edit',
                    del_url: 'task/record_money/del',
                    multi_url: 'task/record_money/multi',
                    table: 'record_money',
                    ajax_url:{
                        btn_1:'task/record_money/remarks_success?type=1',
                        btn_2:'task/record_money/remarks_fail?type=2'
                    },
                    btn_mall_window:{
                        btn_array:["确定","取消"],
                        btn_evn_array_length:2,
                        btn_evn_array: {
                            evn_1:function (index,layero) {
                                var frame = Layer.getChildFrame('html', index);
                                var form = $("form",frame);
                                var success = function (data,ret) {
                                    Layer.msg(data.responseJSON.msg);
                                    table.bootstrapTable('refresh');
                                    Layer.close(index);
                                }
                                Fast.api.form_ajax_submit(form,success);
                            },
                            evn_2:function (index,layero) {
                                Layer.close(index);
                            }
                        },
                        area:['400px','250px']
                    },
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'record_id',
                sortName: 'record_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'sd_shop_order.order_sno', title: __('Order_sno')},
                        {field: 'sd_shop_order.shop_name', title: __('Shop_name'),operate:"LIKE"},
                        {field: 'username', title: __('Username')},
                        {field: 'sd_shop_order.tao_bao_no', title: __('Tao_bao_no')},
                        {field: 'sd_shop_order.tao_bao_order_no', title: __('Tao_bao_order_no')},
                        {field: 'bank_name', title: __('Bank_name')},
                        {field: 'bank_no', title: __('Bank_no')},
                        {field: 'sd_shop_order.real_money', title: __('Order_amount')},
                        {field: 'a.on_time', title: __('On_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'sd_shop_order.status',
                            title: __('Status'),
                            searchList: {
                                "0":__('已拍下'),
                                "1":__('未审核'),
                                "2":__('已审核'),
                                "3":__('已上传'),
                                "4":__('已复查'),
                                "5":__('已付款'),
                                "6":__('已下单'),
                                "7":__('待发货'),
                                "8":__('待好评'),
                                "9":__('已发货'),
                                "10":__('已好评'),
                                "11":__('待追评'),
                                "12":__('已追评'),
                                "13":__('已完成'),
                            },
                            formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                if(value=="" || value==null){
                                    value = value;
                                }else{
                                    value = value.toString();
                                }
                                var index = keys.indexOf(value);
                                value = value === null ? '' : value.toString();
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
                                if (this.operate != false) {
                                    html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
                                }
                                return html;
                            }
                        },
                        {field: 'fail_remarks', title: __('Fail_remarks')},
                        {field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate,formatter: function (value, row, index) {
                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);

                                // buttons.push(
                                //    {
                                //         name: 'btn_1',
                                //         text: "失败备注",
                                //         extend: 'data-toggle="tooltip" data-name="btn_2" data-title="失败备注" ',
                                //         classname: 'btn btn-xs btn-success btn-magic btn-small-window',
                                //     }
                                //     );
                                //console.log(row.order.status);
                                if(row){

                                if(row["sd_shop_order.status"]==2 ||row["sd_shop_order.status"]==4) {

                                    buttons.push(
                                        {
                                            name: 'btn_1',
                                            text: "打款",
                                            extend: 'data-toggle="tooltip" data-name="btn_1" data-title="打款" ',
                                            classname: 'btn btn-xs btn-success btn-magic btn-small-window',
                                        },
                                        {
                                            name: 'btn_1',
                                            text: "失败备注",
                                            extend: 'data-toggle="tooltip" data-name="btn_2" data-title="失败备注" ',
                                            classname: 'btn btn-xs btn-success btn-magic btn-small-window',
                                        }
                                    );
                                }

                                }

                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                            }
                        }
                    ]
                ]
            });

            $(document).on("click", "#paied", function (e){
                var data = table.bootstrapTable('getSelections');
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定批量打款？',{
                        btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                        Fast.api.ajax({
                            url:  'task/record_money/payed',
                            data: {
                                'ids': data,
                            },
                            success: function (data) {
                                console.log(data);
                                table.bootstrapTable('refresh');
                                layer.msg(data.msg);
                            },
                            error: function (data) {
                                table.bootstrapTable('refresh');
                                //失败的回调
                                layer.msg(data.msg);
                            }
                        });
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });



            $(document).on("click", "#export", function (e){
                var data = table.bootstrapTable('getSelections');
                var ids_arr = new Array();
                for(var j=0;j<data.length;j++){
                    ids_arr.push(data[j].id);
                }
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定导出？',{
                        btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                    location.href = '../task/record_money/export_act?ids='+ids_arr.join(',');
                    Layer.close(index);
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});